package com.hw.one.collect.modbus.job;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.hw.one.collect.config.DropTimeConfig;
import com.hw.one.collect.entity.CommonData;
import com.hw.one.collect.modbus.collect.ModbusDefaultDataCollector;
import com.hw.one.transpot.constant.DeviceCodeConstant;
import com.hw.one.transpot.util.TransporterDataUtil;

/**
 * @Class DefaultJob
 * @Author guoshenzhen
 * @Date 2018/3/14 15:59
 */
@Component
public class ModbusDataJob {
	private Logger log = Logger.getLogger(this.getClass());

	public void start() {
		try {
			String dropTime = DropTimeConfig.getDropTime(DeviceCodeConstant.modbusDeviceCode);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
			Calendar c = Calendar.getInstance();
			long now = c.getTime().getTime();
			long latest = sdf.parse(dropTime).getTime();
			int hours = (int) (now - latest) / 3600000;
			String t = "";
			LinkedList<CommonData> cds = new LinkedList<CommonData>();
			for (int j = 0; j < hours; j++) {
				t = sdf.format(c.getTime());
				CommonData result = new ModbusDefaultDataCollector().collect(t);
				if (result != null) {
					result.setTime(t);
					result.setDeviceCode(DeviceCodeConstant.modbusDeviceCode);
					cds.push(result);
				}
				c.add(Calendar.HOUR, -1);
			}
			// 发送数据
			if(cds.size() > 0) {
				TransporterDataUtil util = new TransporterDataUtil();
				util.sendBytes(cds);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}
	
	public static void main(String[] args) {
		ModbusDataJob m = new ModbusDataJob();
		m.start();
	}
}